<?php
header("Content-Type:text/html;charset=utf-8");
session_start();
if($_GET["newEdit"]){
	$date=mktime();
	$info=stripcslashes($_GET["info"]);
	$info_json=json_decode("$info");
	$set=stripcslashes($_GET["set"]);
	$set_json=json_decode("$set");
	$username=$_SESSION["login"];
	$filepath="../user/$username/love.xml";
	$dom=new DOMDocument();
	$dom->load("$filepath");
	$root=$dom->documentElement;
	//添加信息
	//添加内容
	$love=$dom->createElement("love");
	$sub_id=time();
	$love->setAttribute("id",$sub_id);
	$root->appendChild($love);
	//添加文章列表
	$subject=$dom->getElementsByTagName("subject")->item(0);
		$node=$dom->createElement("sub");
		$text=$dom->createTextNode($info_json->header);
		$node->appendChild($text);
		$node->setAttribute("id","$sub_id");
		$subject->appendChild($node);
	
	$node=$dom->createElement("header");
	$text=$dom->createTextNode($info_json->header);
	$node->appendChild($text);
	$love->appendChild($node);
	
	$node=$dom->createElement("tishi");
	$text=$dom->createTextNode($info_json->tishi);
	$node->appendChild($text);
	$love->appendChild($node);
	
	$node=$dom->createElement("start");
	$text=$dom->createTextNode($info_json->start);
	$node->appendChild($text);
	$love->appendChild($node);
	
	$node=$dom->createElement("pp");
	$love->appendChild($node);
	$pp=$info_json->pp;
	foreach ($pp as $p){
		$p_node=$dom->createElement("p");
		$text=$dom->createTextNode($p);
		$p_node->appendChild($text);
		$node->appendChild($p_node);
	}
	
	$node=$dom->createElement("final");
	$text=$dom->createTextNode($info_json->final);
	$node->appendChild($text);
	$love->appendChild($node);
	
	//存储设置
	$set=$dom->createElement("set");
	$love->appendChild($set);
	
	$node=$dom->createElement("background");
	$text=$dom->createTextNode($set_json->background);
	$node->appendChild($text);
	$set->appendChild($node);
	
	if($set_json->music!=""){
		$node=$dom->createElement("music");
		$text=$dom->createTextNode($set_json->music);
		$node->appendChild($text);
		$set->appendChild($node);
		
		$node=$dom->createElement("musicrepeat");
		$text=$dom->createTextNode($set_json->musicrepeat);
		$node->appendChild($text);
		$set->appendChild($node);
	}
	$dom->save("$filepath");
	echo "ok";
}else if ($_GET["yulanEdit"]){
	$info=stripcslashes($_GET["info"]);
	$set=stripcslashes($_GET["set"]);
	$str=$info."__yulan__".$set;
	file_put_contents("yulan.txt","$str");
	echo "ok";
}else if($file=$_GET["again_upload"]){
	unlink($file);
	$name=$_GET["go"];
	echo "<script type='text/javascript'>location.href='../basic/upload.php?go=$name'</script>";
}
function unloadfile($name,$path,$filetype){
	if($_POST["$name"."_file"]){
		if($_FILES["$name"]["error"]==0){
			$type=$_FILES["$name"]["type"];
			if(preg_match("/$filetype/",$type)){
				$type=substr($type,6);
				$filename=time();
				$user=$_SESSION["login"];
				$filepath="../user/$user/$path/$filename.mp3";
				if(is_uploaded_file($_FILES["$name"]["tmp_name"])){
					if(move_uploaded_file($_FILES["$name"]["tmp_name"], $filepath)){
						echo $filepath;
						echo "<a href='edit_stage.php?again_upload=$filepath&go=$name'>重新上传</a>";
					}else{
						echo "fail";
					}
				}else{
					echo "no access";
				}
			}
		}else{
			echo $_FILES["$name"]["error"];
		}
	}
}
unloadfile("bgimage","img","image");
unloadfile("bgmusic","mp3","audio");
?>
